package edu.pku.sei.transformation.atl.adapter;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.core.MetaNamedElement;
import edu.pku.sei.modeler.model.core.MetaPrimitiveType;
import edu.pku.sei.transformation.atl.rule.IATLType;
import edu.pku.sei.transformation.atl.rule.IATLVariable;
import edu.pku.sei.transformation.editor.model.variable.PrimitiveTypeVariable;
import edu.pku.sei.transformation.editor.model.variable.Variable;
import edu.pku.sei.transformation.editor.views.problem.ProblemView;

public class ATLVariableAdapter implements IATLVariable {
	protected Variable variable = null;
	private IATLType atlType = null;
	
	public ATLVariableAdapter(Variable variable) throws Exception {
		this.variable = variable;
		MetaModelElement type = variable.getType();
		if (type instanceof MetaPrimitiveType) {
			String name = ((MetaPrimitiveType)type).getName();
			if (name.equals("int")) {
				atlType = ATLPrimitiveType.INTEGER_TYPE;
			} else if (name.equals("String")) {
				atlType = ATLPrimitiveType.STRING_TYPE;
			} else if (name.equals("float")) {
				
			} else {
				ProblemView.addError(variable.getModel(), ProblemView.CODEGEN_ATL, "primitive type", "not support primitive type" + name + "right now", variable.getContainer().toString()+"->"+variable.getName());
				throw new Exception("type do not support: " + name);
			}
		} else if (type instanceof MetaNamedElement){
			atlType = new ATLModelTypeAdapter((MetaNamedElement) type);
		} else {
			ProblemView.addError(variable.getModel(), ProblemView.CODEGEN_ATL, "primitive type", "not support type " + type.getClass().toString(), variable.getContainer().toString()+"->"+variable.getName());
			throw new Exception("type do not support: "+ type.getClass().getName());
		}	
	}
	
	@Override
	public String getName() {
		return variable.getName();
	}

	@Override
	public IATLType getType() {
		return atlType;
	}

	@Override
	public String getInitValue() {
		if (variable instanceof PrimitiveTypeVariable) {
			return ((PrimitiveTypeVariable)variable).getInitValue();
		} else if (atlType instanceof ATLModelTypeAdapter) {
			return "null";
		} else if (atlType == ATLPrimitiveType.INTEGER_TYPE) {
			return "0";
		} else if (atlType == ATLPrimitiveType.STRING_TYPE) {
			return "\"\"";
		} else if (atlType == ATLPrimitiveType.REAL_TYPE) {
			return "0.0";
		} else {
			return "null";
		}
	}

}
